<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">

    </ui:define>

    <ui:define name="right">

    </ui:define>

    <ui:define name="content">
        <style>
            .ui-state-highlight.ui-selected {
                color: blueviolet;
            }
        </style>

        <h2>Create Sales Invoice</h2>

        <h:form id="searchSOForm" >
            <p:panel> 
                <p:messages />

                <h:panelGrid columns="3" cellpadding="10">
                    <h:outputLabel for="salesOrderIdStr" style="font-weight: bold" value="Enter Sales Order Id: "/>
                    <h:inputText id="salesOrderIdStr" value="#{sbpInvoiceManagerBean.salesOrder.salesOrderIdStr}">
                        <p:ajax update="searchSOForm:deliveryOrders"/>
                    </h:inputText>
                    <p:commandButton oncomplete="soDialog.show();" image="ui-icon ui-icon-search"/>

                    <h:outputLabel value=""/>
                    <h:outputLabel value=""/>
                    <h:outputLabel value=""/>
                </h:panelGrid>

                <h:panelGroup id="deliveryOrders" style="width: 100%" rendered="#{sbpInvoiceManagerBean.salesOrder.salesOrderIdStr.length() != 0}">

                    <h:outputText value="Delivery Order" style="font-weight: bold; font-size: 110%"/>
                    <p:separator title="Delivery Order"/> 
                    <p:dataTable id="olTable" var="deliveryOrderRow" rowIndexVar="rowIndex" value="#{sbpInvoiceManagerBean.salesOrder.deliveryOrders}" emptyMessage="No order lines is added to Sales Order" style="border-color: transparent; width: 100%">  
                        <p:column style="border-color: transparent">
                            <br/>
                            <p:fieldset legend="Delivery Order #{rowIndex + 1}">  

                                <h:panelGrid id="olpanelgrid" columns="2" style="border-color: transparent">
                                    <h:outputLabel style="font-weight: bold" value="Delivery Order Id: "/>
                                    <h:outputText value="#{deliveryOrderRow.deliveryOrderIdStr}" />
                                    <h:outputLabel style="font-weight: bold" value="Lead Time: "/>
                                    <h:outputText value="#{deliveryOrderRow.leadTime}" />
                                    <h:outputLabel style="font-weight: bold" value="Shipping Address: "/>
                                    <h:outputText value="#{deliveryOrderRow.shippingAddress.addressLine1}" />
                                    <h:outputLabel style="font-weight: bold" value=""/>
                                    <h:outputText value="#{deliveryOrderRow.shippingAddress.addressLine2}" />
                                    <h:outputLabel style="font-weight: bold" value=""/>
                                    <h:outputText value="#{deliveryOrderRow.shippingAddress.city}" />
                                    <h:outputLabel style="font-weight: bold" value=""/>
                                    <h:outputText value="#{deliveryOrderRow.shippingAddress.stateName}" />
                                    <h:outputLabel style="font-weight: bold" value=""/>
                                    <h:outputText value="#{deliveryOrderRow.shippingAddress.country}" />
                                    <h:outputLabel style="font-weight: bold" value=""/>
                                    <h:outputText value="#{deliveryOrderRow.shippingAddress.zipCode}" />
                                </h:panelGrid>
                                <p:dataTable id="olilTable" var="il" value="#{deliveryOrderRow.itemLines}" emptyMessage="No products selected">  
                                    <p:column rendered="#{il.qtyOrderedCase != 0}">  
                                        <f:facet name="header" >  
                                            Item Name
                                        </f:facet>  
                                        <h:outputText value="#{il.product.productType}" />  
                                    </p:column>
                                    <p:column rendered="#{il.qtyOrderedCase != 0}">
                                        <f:facet name="header">
                                            Quantity Ordered 
                                            <br />
                                            Case
                                        </f:facet>
                                        <h:outputText value="#{il.qtyOrderedCase}" />
                                    </p:column>
                                </p:dataTable>
                                <br/>
                            </p:fieldset>
                            <br/>
                            <div align="right">
                                <p:commandButton value="Invoice Created" disabled="true" rendered="#{deliveryOrderRow.invoice != null}"/>
                                <p:commandButton value="Create Invoice" action="#{sbpInvoiceManagerBean.createInvoice(deliveryOrderRow.id)}" rendered="#{deliveryOrderRow.invoice == null}"/>
                            </div>
                        </p:column>
                    </p:dataTable>
                    <br/>
                </h:panelGroup>

                <br/>
            </p:panel>
            <br/>
            <br/>
            <br/>
        </h:form>


        <p:dialog header="searchSalesOrder" widgetVar="soDialog" hideEffect="explode" height="600" width="800">    
            <h:form id="soForm">
                <p:dataTable var="salesOrder" value="#{ssoManagerBean.sos}"
                             emptyMessage="No Sales Orders found with given criteria" selection="#{sbpInvoiceManagerBean.salesOrder}" selectionMode="single" widgetVar="sotbl">

                    <f:facet name="header">
                        <p:outputPanel>
                            <h:outputText value="Search all fields:" />
                            <p:inputText id="globalFilter" onkeyup="sotbl.filter()" style="width:150px" />
                        </p:outputPanel>
                    </f:facet>

                    <p:column filterBy="#{salesOrder.salesOrderIdStr}" 
                              headerText="Sales Order Id" footerText="contains"
                              filterMatchMode="contains">
                        <h:outputText value="#{salesOrder.salesOrderIdStr}" />
                    </p:column>

                    <p:column filterBy="#{salesOrder.purchaseOrder.poNumber}" 
                              headerText="PO Number" footerText="contains"
                              filterMatchMode="contains">
                        <h:outputText value="#{salesOrder.purchaseOrder.poNumber}" />
                    </p:column>

                    <p:column filterBy="#{salesOrder.customer.clientId}"
                              headerText="Client Id" footerText="contains">
                        <h:outputText value="#{salesOrder.customer.clientId}" />
                    </p:column>

                    <p:column filterBy="#{salesOrder.customer.companyName}"
                              headerText="Client Name" footerText="contains">
                        <h:outputText value="#{salesOrder.customer.companyName}" />
                    </p:column>

                    <p:column filterBy="#{salesOrder.customer.category}" 
                              headerText="Customer Category" footerText="exact"
                              filterOptions="#{ssoManagerBean.categoryOptions}"
                              filterMatchMode="exact">
                        <h:outputText value="#{salesOrder.customer.category}" />
                    </p:column>

                    <p:column filterBy="#{salesOrder.dateStr}" 
                              headerText="Sales Order Date" footerText="contains"
                              filterMatchMode="contains">
                        <h:outputText value="#{salesOrder.dateStr}" />
                    </p:column>

                    <p:column filterBy="#{salesOrder.salesOrderStatus}"
                              headerText="Sales Order Status" footerText="exact"
                              filterOptions="#{ssoManagerBean.statusOptions}"
                              filterMatchMode="exact">
                        <outputText value="#{salesOrder.salesOrderStatus}" />
                    </p:column>
                </p:dataTable>
                <p:commandButton value="Select Sales Order" update="searchSOForm" oncomplete="soDialog.hide()"/>
            </h:form>
        </p:dialog>
    </ui:define>

    <ui:define name="bottom">

    </ui:define>

</ui:composition>
